package com.me.service;

import com.me.mapper.PokemonMapper;
import com.me.mapper.TrainerMapper;
import com.me.pojo.Pokemon;
import com.me.pojo.Trainer;
import com.me.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class TrainerService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 查询所有
     *
     * @return
     */
    public List<Trainer> selectall() {
        //调用BrandMapper.selectall()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        //4. 调用方法
        List<Trainer> trainers = mapper.selectall();

        sqlSession.close();

        return trainers;
    }

    /**
     * 添加
     *
     * @param trainer
     * @return
     */
    public void addtrainer(Trainer trainer) {

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        //4. 调用方法
        mapper.addtrainer(trainer);

        //提交事务
        sqlSession.commit();

        //释放资源
        sqlSession.close();

    }

    public Trainer selecttrainerbyid(int id) {
        //调用PokemonMapper.selectbyid()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        //4. 调用方法
        Trainer trainers = mapper.selecttrainerbyid(id);

        sqlSession.close();

        return trainers;
    }

    public void updatetrainer(Trainer trainer) {
        //调用PokemonMapper.pdatepokemon()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3. 获取BrandMapper
       TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        //4. 调用方法
        mapper.updatetrainer(trainer);

        //提交事务
        sqlSession.commit();

        //5.释放资源

        sqlSession.close();

    }
    public void deletetrainer(int id) {
        //调用PokemonMapper.selectbyid()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        //4. 调用方法
        mapper.deletetrainer(id);

        //提交事务
        sqlSession.commit();

        sqlSession.close();

    }
    public Integer trainerCount() {
        //调用PokemonMapper.pokemonCount()
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        //4. 调用方法
        Integer count = mapper.trainerCount();

        //5.释放资源
        sqlSession.close();

        return count;
    }
    public List<Trainer> searchTrainerBykeyword(String keyword){

        SqlSession sqlSession = factory.openSession();

        TrainerMapper mapper = sqlSession.getMapper(TrainerMapper.class);

        List<Trainer> trainers = mapper.searchTrainerBykeyword(keyword);

        sqlSession.close();

        return trainers;
    }

    }
